package com.meishij.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.util.Pools;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.drawee.components.DraweeEventTracker;
import com.facebook.drawee.view.SimpleDraweeView;
import com.meishij.MallSellImageActivity;
import com.meishij.entry.MallSellEntry;

import java.io.Serializable;
import java.util.List;

/**
 * Created by 肖荣 on 2016/10/14.
 */

public class MallSellViewPagerAdapter extends PagerAdapter {
    private Context context;
    private List<MallSellEntry.ObjBean.ImagesBean> imagesList;
    private Pools.Pool<SimpleDraweeView> pool=new Pools.SimplePool<>(4);
    public MallSellViewPagerAdapter(Context context, List<MallSellEntry.ObjBean.ImagesBean> imagesBeanList) {
        this.context = context;
        this.imagesList = imagesBeanList;
        MallSellEntry.ObjBean.ImagesBean first = imagesBeanList.get(0);
        MallSellEntry.ObjBean.ImagesBean last = imagesBeanList.get(imagesBeanList.size() - 1);
        imagesList.add(0, last);
        imagesList.add(first);
    }

    @Override
    public int getCount() {
        return imagesList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view.equals(object);
    }

    @Override
    public Object instantiateItem(final ViewGroup container, final int position) {
        SimpleDraweeView simpleDraweeView = pool.acquire();
        if(simpleDraweeView==null){
            simpleDraweeView=new SimpleDraweeView(context);
        }
        simpleDraweeView.setImageURI(imagesList.get(position).getSmall());
        simpleDraweeView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, MallSellImageActivity.class);
                intent.putExtra("images", ((Serializable) imagesList));
                intent.putExtra("position",position);
                context.startActivity(intent);
            }
        });
        container.addView(simpleDraweeView);
        return simpleDraweeView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
        pool.release(((SimpleDraweeView) object));
    }
}
